package com.cn.wanxi.servlet.back.news;

import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.INewsService;
import com.cn.wanxi.service.impl.NewsServiceImpl;
import com.cn.wanxi.service.impl.NewsTypeServiceImpl;
import com.cn.wanxi.util.DataEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/news/add")
public class NewsAddServlet extends HttpServlet {
    INewsService newsService = new NewsServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setTextContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //新闻添加
        String newstype = req.getParameter("newstype");
        int ntid = 0;
        if (!"".equals(newstype)) {
            ntid = Integer.parseInt(newstype);
        }
        NewsModel news = new NewsModel();
        news.setTitle(req.getParameter("title"));
        news.setContent(req.getParameter("content"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = sdf.format(new Date().getTime());
        news.setCreate_time(date);
        news.setNewsType(new NewsTypeServiceImpl().getNewsType(ntid));
        int result = newsService.addNews(news);
        resp.getWriter().print(result);
    }
}
